<?php

/**
 * 数据统计控制器
 */

namespace App\Http\ManagerApi\Controllers;

use App\Api\BaseClass\Controller;
use App\Http\WechatAuth\Models\User;
use App\Http\WechatAuth\Models\UserCashRequestLog;
use Illuminate\Support\Facades\DB;
use App\Http\ManagerApi\Models\UserIntegralLog;
use Carbon\Carbon;
use App\Http\ManagerApi\Models\CashRequestLog;

class InformationCountController extends Controller
{
    /**
     * 用户数据
     */
    public function userCount()
    {
        $data['total_user'] = User::where('is_active', 1)->count();
        $data['day_user'] = User::where('is_active', 1)->whereDate('created_at', date('Y-m-d'))->count();
        $w = date('W');
        $data['week_user'] = User::where('is_active', 1)->whereRaw('date_format(created_at,\'%u\') = ?', [$w])->count();
        $data['month_user'] = User::where('is_active', 1)->whereYear('created_at', date('Y'))->whereMonth('created_at', date('m'))->count();
        return $this->form->datas($data);
    }

    /**
     * 提现数据
     */
    public function cashCount()
    {
        $data['total_money'] = UserCashRequestLog::where('status', 1)->sum('changed_integral');
        $data['day_money'] = UserCashRequestLog::where('status', 1)->whereDate('updated_at', date('Y-m-d'))->sum('changed_integral');
        $w = date('W');
        $data['week_money'] = UserCashRequestLog::where('status', 1)->whereRaw('date_format(updated_at,\'%u\') = ?', [$w])->sum('changed_integral');
        $data['month_money'] = UserCashRequestLog::where('status', 1)->whereYear('updated_at', date('Y'))->whereMonth('updated_at', date('m'))->sum('changed_integral');
        return $this->form->datas($data);
    }

    /**
     * 平台金额数据
     */
    public function moneyCount()
    {
        $data['total_money'] = UserIntegralLog::where('is_finished', 1)
            ->where('flag', 1)
            ->whereIn('type', [1, 2, 3, 4, 5, 6])
            ->sum('changed_integral');

        $data['day_money'] = UserIntegralLog::whereDate('updated_at', date('Y-m-d'))
            ->where('is_finished', 1)
            ->where('flag', 1)
            ->whereIn('type', [1, 2, 3, 4, 5, 6])
            ->sum('changed_integral');

        $w = date('W');
        $data['week_money'] = UserIntegralLog::whereRaw('date_format(updated_at,\'%u\') = ?', [$w])
            ->where('is_finished', 1)
            ->where('flag', 1)
            ->whereIn('type', [1, 2, 3, 4, 5, 6])
            ->sum('changed_integral');

        $data['month_money'] = UserIntegralLog::whereYear('updated_at', date('Y'))
            ->whereMonth('updated_at', date('m'))
            ->where('is_finished', 1)
            ->where('flag', 1)
            ->whereIn('type', [1, 2, 3, 4, 5, 6])
            ->sum('changed_integral');

        return $this->form->datas($data);
    }

    /**
     * 财务统计
     */
    public function financeStatistics()
    {
        $return = [];

        // 总分红
        $return['totalBonus'] = UserIntegralLog::where('flag', 1)
            ->where('is_finished', 1)
            ->whereIn('type', [1, 2, 3, 4, 5, 6])
            ->sum('changed_integral');

        // 总提现
        $return['totalCash'] = CashRequestLog::where('status', 1)
            ->sum('changed_integral');

        // 总拨出
        $return['totalTransfer'] = UserIntegralLog::where('type', 8)
            ->where('flag', 1)
            ->where('is_finished', 1)
            ->sum('changed_integral');

        // 总管理费
        $return['totalManageFee'] = UserIntegralLog::where('type', 10)
            ->where('is_finished', 1)
            ->sum('changed_integral');

        // 总服务费
        $return['totalServeFee'] = UserIntegralLog::where('type', 5)
            ->where('is_finished', 1)
            ->sum('changed_integral');

        return $this->form->datas($return);
    }
    /**
     * 财务明细
     * 总分红 提现 转让 平台管理费
     */
    public function financeDetail()
    {
        $rules = [
            ['offset', 'required|integer'],
            ['limit', 'required|integer'],
            ['type', 'integer'],
            ['start', 'date'],
            ['end', 'date'],
        ];

        $params = $this->form->camelFormOrFail($rules);
        $search_params = [
            ['orderBy', 'created_at', 'desc'],
            ['orderBy', 'id', 'desc'],
            ['where', 'created_at', '>=', '$start'],
            ['where', 'created_at', '<=', '$end'],
            ['with', 'user']
        ];

        if (isset($params['type']) && $params['type'] == 666) {
            $search_params[] = ['whereIn', 'type', [1, 2, 3, 4, 5, 6]];
        } elseif (isset($params['type'])) {
            $search_params[] = ['where', 'type', '$type'];
            if ($params['type'] == 8) {
                $search_params[] = ['where', 'flag', 1];
            }
        }

        $userintegrallog = (new UserIntegralLog)->pagination($params, $search_params);

        return $this->form->getMessage($userintegrallog);
    }

    /**
     * 本周新增用户
     */
    public function userCountWeek()
    {
        $return = [];

        $cur_weekday = date('N');
        for ($i = 1; $i <= 7; $i++) {

            if ($cur_weekday >= $i) {

                $return[] = User::whereDate('created_at', Carbon::now()->subDays($cur_weekday - $i)->toDateString())->where('is_active', 1)->count();
            } else {

                $return[] = 0;
            }
        }

        return $this->form->datas($return);
    }

    /**
     * 本月新增用户
     */
    public function userCountMonth()
    {
        $return = [];

        $month_days = date('t');
        $cur_days = date('j');
        for ($i = 1; $i <= $month_days; $i++) {

            if ($cur_days >= $i) {

                $date = Carbon::now()->subDays($cur_days - $i)->toDateString();

                $arr['date'] = $date;
                $arr['num'] = User::whereDate('created_at', $date)->where('is_active', 1)->count();
            } else {

                $date = Carbon::now()->addDays($i - $cur_days)->toDateString();

                $arr['date'] = $date;
                $arr['num'] = 0;
            }

            $return[] = $arr;
        }

        return $this->form->datas($return);
    }

    /**
     * 本周新增提现
     */
    public function cashCountWeek()
    {
        $return = [];

        $cur_weekday = date('N');
        for ($i = 1; $i <= 7; $i++) {

            if ($cur_weekday >= $i) {

                $return[] = CashRequestLog::whereDate('updated_at', Carbon::now()->subDays($cur_weekday - $i)->toDateString())
                    ->where('status', 1)
                    ->sum('changed_integral');
            } else {

                $return[] = 0;
            }
        }

        return $this->form->datas($return);
    }

    /**
     * 本月新增提现
     */
    public function cashCountMonth()
    {
        $return = [];

        $month_days = date('t');
        $cur_days = date('j');
        for ($i = 1; $i <= $month_days; $i++) {

            if ($cur_days >= $i) {

                $date = Carbon::now()->subDays($cur_days - $i)->toDateString();

                $arr['date'] = $date;
                $arr['num'] = CashRequestLog::whereDate('updated_at', $date)
                    ->where('status', 1)
                    ->sum('changed_integral');
            } else {

                $date = Carbon::now()->addDays($i - $cur_days)->toDateString();

                $arr['date'] = $date;
                $arr['num'] = 0;
            }

            $return[] = $arr;
        }

        return $this->form->datas($return);
    }

    /**
     * 本周新增金额
     */
    public function moneyCountWeek()
    {
        $return = [];

        $cur_weekday = date('N');
        for ($i = 1; $i <= 7; $i++) {

            if ($cur_weekday >= $i) {

                $return[] = CashRequestLog::whereDate('updated_at', Carbon::now()->subDays($cur_weekday - $i)->toDateString())
                    ->where('status', 1)
                    ->sum('changed_integral');
            } else {

                $return[] = 0;
            }
        }

        return $this->form->datas($return);
    }

    /**
     * 本月新增金额
     */
    public function moneyCountMonth()
    {
        $return = [];

        $month_days = date('t');
        $cur_days = date('j');
        for ($i = 1; $i <= $month_days; $i++) {

            if ($cur_days >= $i) {

                $date = Carbon::now()->subDays($cur_days - $i)->toDateString();

                $arr['date'] = $date;
                $arr['num'] = CashRequestLog::whereDate('updated_at', $date)
                    ->where('status', 1)
                    ->sum('changed_integral');
            } else {

                $date = Carbon::now()->addDays($i - $cur_days)->toDateString();

                $arr['date'] = $date;
                $arr['num'] = 0;
            }

            $return[] = $arr;
        }

        return $this->form->datas($return);
    }
}